﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZWL.BLL;
using System.Data;

namespace YZT.Common
{
    public partial class FlowNext : System.Web.UI.Page
    {
        public string WORKFLOWID;
        public string JIEDIANSERILS;
        public string PROJECTID;
        public string USER;
        protected void Page_Load(object sender, EventArgs e)
        {
            WORKFLOWID = Request["WORKFLOWID"];
            JIEDIANSERILS = Request["JIEDIANSERILS"];
            PROJECTID = Request["PROJECT_ID"];
            USER = ZWL.Common.PublicMethod.GetSessionValue("UserName");
            if (!Page.IsPostBack)
            {
                ZWL.BLL.ERPWorkFlowJieDian jd = new ZWL.BLL.ERPWorkFlowJieDian();
                string jdid = Helper.CommonHelper.GetFieldValueBySQL("ERPWORKFLOWJIEDIAN", "ID", "WORKFLOWID=" + WORKFLOWID.ToString() + " AND JIEDIANSERILS='" + JIEDIANSERILS + "'");
                if (jdid != string.Empty)
                {
                    jd.GetModel(int.Parse(jdid));
                    jd.ID = int.Parse(jdid);
                    string nextserils = jd.NextJieDianSerils;
                    string[] nserils = nextserils.Split(',');
                    for (int i = 0; i < nserils.Length; i++)
                    {

                        ERPWorkFlowJieDian zjd = new ERPWorkFlowJieDian();
                        int zjdid = int.Parse(Helper.CommonHelper.GetFieldValueBySQL("ERPWORKFLOWJIEDIAN", "ID", "WORKFLOWID=" + WORKFLOWID.ToString() + " and JieDianSerils='" + nserils[i] + "'"));
                        zjd.GetModel(zjdid);
                        zjd.ID = zjdid;
                        TreeNode node1 = new TreeNode();
                        node1.Text = zjd.JieDianName;
                        node1.Value = zjd.JieDianSerils;
                        TreeView1.Nodes.Add(node1);
                        string users = zjd.JingBanRenYuanList;
                        string[] us = users.Split(',');
                        for (int j = 0; j < us.Length; j++)
                        {
                            TreeNode node2 = new TreeNode();
                            node2.Text = us[j];
                            node2.Value = us[j];
                            node2.ShowCheckBox = true;
                            node1.ChildNodes.Add(node2);
                            //node2.Checked = true;
                        }
                    }
                }
            }
        }

        protected void buttoncancel_Click(object sender, EventArgs e)
        {
            Response.Write("<script language=javascript>history.go(-2);</script>");
        }

        protected void buttonok_Click(object sender, EventArgs e)
        {
            string flowuser = "";
            int count = 0;

            string curprjid = Helper.CommonHelper.GetFieldValueBySQL("ERPXMLB", "ID", "YXBS=1 AND JIEDIANSERILS='"+JIEDIANSERILS+"' AND PROJECT_ID='" + PROJECTID + "' and workflowid=" + WORKFLOWID);
            ERPXMLB cprj = new ERPXMLB();
            cprj.GetModel(int.Parse(curprjid));
            cprj.ID = int.Parse(curprjid);
            if (cprj.YXBS == 1)
            {
                cprj.YXBS = 0;
                cprj.Update();//将当前的YXBS设置为0

                bool byrtg = true;//一人通过可向下流转

                //把这个环节对应的其他的人也处理了，分为两种情况，一种是一人通过全部通过，另一种是全部通过才行
                string hjid = Helper.CommonHelper.GetFieldValueBySQL("erpworkflowjiedian", "ID", "workflowid=" + WORKFLOWID + " and jiedianserils='" + JIEDIANSERILS + "'");
                if (hjid != null && hjid != string.Empty)
                {
                    ERPWorkFlowJieDian jied = new ERPWorkFlowJieDian();
                    jied.GetModel(int.Parse(hjid));
                    jied.ID = int.Parse(hjid);
                    if (jied.PingShenMoShi == "一人通过可向下流转")
                    {
                        byrtg = true;
                        string sss = ZWL.DBUtility.DbHelperSQL.GetStringList("select id from erpxmlb where workflowid=" + WORKFLOWID + " and project_id='" + PROJECTID + "' and jiedianserils='" + JIEDIANSERILS + "' and dangqianuser<>'" + USER + "'");
                        if (sss != null && sss != string.Empty)
                        {
                            string[] ssss = sss.Split('|');
                            if (ssss != null && ssss.Length > 0)
                            {
                                for (int i = 0; i < ssss.Length; i++)
                                {
                                    int id = int.Parse(ssss[i]);
                                    ERPXMLB l = new ERPXMLB();
                                    l.GetModel(id);
                                    l.ID = id;
                                    l.YXBS = 0;
                                    l.Update();
                                }
                            }
                        }
                    }
                    else
                    {
                        byrtg = false;
                    }
                }

                //多个环节
                for (int i = 0; i < TreeView1.Nodes.Count; i++)
                {
                    TreeNode node = TreeView1.Nodes[i];//节点node

                    //多个人员
                    for (int j = 0; j < node.ChildNodes.Count; j++)
                    {
                        if (node.ChildNodes[j].Checked == true)
                        {
                            count++;
                            string username = node.ChildNodes[j].Text;
                            string jiedianserils = node.Value;
                            flowuser += username + ",";
                            //下个人的项目列表
                            string prjid = Helper.CommonHelper.GetFieldValueBySQL("ERPXMLB", "ID", "PROJECT_ID='" + PROJECTID + "' and workflowid=" + WORKFLOWID + " and dangqianuser='" + username + "' and jiedianserils='" + jiedianserils + "'");

                            ERPXMLB lb = new ERPXMLB();
                            bool bexists = false;
                            if (prjid != string.Empty)
                            {
                                lb.GetModel(int.Parse(prjid));
                                lb.ID = int.Parse(prjid);
                                bexists = true;
                            }
                            else
                            {
                                lb.ChuangJianRen = cprj.ChuangJianRen;
                                lb.ChuangJianSJ = cprj.ChuangJianSJ;
                                lb.DangQianUser = username;
                                lb.FormID = cprj.FormID;
                                lb.FormName = cprj.FormName;
                                lb.JieDianName = node.Text;
                                lb.JieDianSerils = jiedianserils;
                                lb.PROJECT_ID = PROJECTID;
                                lb.PROJECT_NAME = cprj.PROJECT_NAME;
                                lb.WorkFlowID = cprj.WorkFlowID;
                                lb.WorkFlowName = cprj.WorkFlowName;
                                lb.ZhuanRuJDSerils = JIEDIANSERILS;
                                lb.YXBS = 1;
                            }
                            lb.YXBS = 1;
                            //判断是否可办理
                            List<string> listNextNodeSerils = new List<string>();
                            GetPreSerils(listNextNodeSerils, jiedianserils,WORKFLOWID);
                            string str = "(";
                            if (listNextNodeSerils != null && listNextNodeSerils.Count > 0)
                            {
                                for (int k = 0; k < listNextNodeSerils.Count; k++)
                                {
                                    str += "'" + listNextNodeSerils[k] + "',";
                                }
                                str = str.Substring(0, str.Length - 1);
                            }
                            str += ")";

                            bool bBB = ZWL.DBUtility.DbHelperSQL.Exists("SELECT * FROM ERPXMLB WHERE WORKFLOWID=" + WORKFLOWID + " AND PROJECT_ID='" + PROJECTID + "' AND YXBS>0 AND JIEDIANSERILS IN " + str);
                            lb.SFKBL = bBB ? 0 : 1;
                            if (bexists)
                                lb.Update();
                            else
                                lb.Add();
                        }
                    }
                }
                if (flowuser != string.Empty && flowuser != "")
                {
                    flowuser = flowuser.Substring(0, flowuser.Length - 1);
                }
                ZWL.Common.MessageBox.ShowAndRedirect(this, "成功转给" + flowuser + "！", "ProjectListInner.aspx");
            }
            else
            {
                ZWL.Common.MessageBox.ShowAndRedirect(this, "该项目已经被其他人办理，已经失效!", "ProjectListInner.aspx");
            }
        }


        private void GetNextSerils(List<string> list, string serils)
        {
            string nextserils = Helper.CommonHelper.GetFieldValueBySQL("ERPWORKFLOWJIEDIAN", "NextJieDianSerils", "JIEDIANSERILS='" + serils + "'");
            if (nextserils != null && nextserils != string.Empty && nextserils != "")
            {
                string[] nss = nextserils.Split(',');
                for (int i = 0; i < nss.Length; i++)
                {
                    if (!list.Contains(nss[i]))
                    {
                        list.Add(nss[i]);
                        GetNextSerils(list, nss[i]);
                    }
                }
            }
        }
        private void GetPreSerils(List<string> list, string serils,string workflowid)
        {
            string str = ZWL.DBUtility.DbHelperSQL.GetStringList("select jiedianserils from ERPWorkFlowJieDian where workflowid=" + workflowid + " and ','+nextjiedianserils+',' like '%," + serils + ",%'");
            if (str != null &&  str != string.Empty && str!="")
            {
                string[] strs = str.Split('|');
                if (strs != null && strs.Length > 0)
                {
                    for (int i = 0; i < strs.Length; i++)
                    {
                        if (!list.Contains(strs[i]))
                        {
                            list.Add(strs[i]);
                            GetPreSerils(list, strs[i],workflowid);
                        }
                    }
                }
            }
        }
    }
}